package pers.xukai.common.entity.po;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import pers.xukai.common.constant.GlobalConstant;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;

/**
 * 用户
 *
 * @author ch-xukai
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "user")
@Table(name = "user", uniqueConstraints = {
        @UniqueConstraint(name = "phone_num_uni", columnNames = "phone_num"),
        @UniqueConstraint(name = "email_uni", columnNames = "email"),
        @UniqueConstraint(name = "wechat_uni", columnNames = "wechat")
})
public class User implements UserDetails {
    private static final long serialVersionUID = 8588059147687792022L;
    /**
     * id
     */
    @Id
    @GeneratedValue(generator = GlobalConstant.ID_GENERATOR_NANO)
    @GenericGenerator(name = GlobalConstant.ID_GENERATOR_NANO, strategy = GlobalConstant.ID_GENERATOR_NANO_PATH)
    private String userId;

    /**
     * 用户名
     */
    @Column(name = "user_name")
    private String userName;

    /**
     * 密码
     */
    @Column(name = "password")
    private String password;

    /**
     * 手机号码
     */
    @Column(name = "phone_num")
    private String phoneNum;

    /**
     * 微信
     */
    @Column(name = "wechat")
    private String wechat;

    /**
     * 电子邮箱
     */
    @Column(name = "email")
    private String email;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @JsonFormat(pattern = "yyyy-MM-dd hh-mm-ss")
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @JsonFormat(pattern = "yyyy-MM-dd hh-mm-ss")
    private Date updateTime;

    /**
     * 创建用户
     */
    @Column(name = "create_user")
    private String createUser;

    /**
     * 更新用户
     */
    @Column(name = "update_user")
    private String updateUser;

    /**
     * 是否锁定
     */
    @Column(name = "is_lock")
    private Boolean isLock;

    /**
     * 是否启用
     */
    @Column(name = "is_enable")
    private Boolean isEnable;

    @OneToMany(targetEntity = Role.class)
    private Collection<GrantedAuthority> roles;

    /**
     * 返回用户权限
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.roles;
    }

    /**
     * 返回用户名
     *
     * @return
     */
    @Override
    public String getUsername() {
        return this.userName;
    }

    /**
     * 指示用户的帐户是否已过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指示用户是被锁定还是解锁
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return this.isLock == null || this.isLock;
    }

    /**
     * 指示用户是被锁定还是解锁
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 指示用户是被锁定还是解锁
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return this.isEnable == null || this.isEnable;

    }
}
